export default class MapBoxSync {

  /**
   * 构造函数
   * @param map  mapboxgl
   */
  constructor(ms) {
    this.ms = ms;
    this.fn = undefined;
    this.args = undefined;
    this.index = 0;
    // this.s1 = this.s1.bind(this);
    // this.n1();
  }


  left(fn, args) {
    this.fn = fn;
    this.args = args;
    this.index = 0;
    return this;
  }

  right(fn, args) {
    this.fn = fn;
    this.args = args;
    this.index = 1;
    return this;
  }

  all(fn, args) {
    this.fn = fn;
    this.args = args;
    this.index = -1;
    return this;
  }

  map(fn, args, index) {
    this.fn = fn;
    this.args = args;
    this.index = index;
    return this;
  }

  async commit() {
    let obj = [];
    if (Array.isArray(this.args)) {
      for (let i = 0; i < this.ms.length; i++) {
        if (!this.ms[i][this.fn]) {
          throw ('can not find ' + this.fn + 'method!')
        }
        if (this.index === -1) {
          obj.push(await this.ms[i][this.fn].apply(this.ms[i], this.args));
        } else {
          if (this.index === i) {
            obj.push(await this.ms[i][this.fn].apply(this.ms[i], this.args));
          }
        }
      }
    } else {
      throw ('Paramter must be a array,please check it!')
    }
    return obj;
  }

  on(event, fn, context) {
    for (let i = 0; i < this.ms.length; i++) {
      this.ms[i].on(event, fn, context);
    }
  }

  off(event, fn) {
    for (let i = 0; i < this.ms.length; i++) {
      this.ms[i].off(event, fn);
    }
  }


  getMap(index) {
    return this.ms[index].getMap();
  }

  getMS(index) {
    return this.ms[index];
  }


}










